<!--最短路径搜索页-->
<template>
  <div class="searchPage">
    <div class="searchPage-topNavigationBar">
      <div class="topNavigationBar-return" @click="cancel">
        <em class="iconfont icon-fanhui" />
      </div>
      <h1 class="topNavigationBar-title">最短路径发现</h1>
      <div></div>
    </div>
    <div class="searchPage-list">
      <div class="list-search">
        <form action="/search">
          <van-search
            v-model.trim="inputValue"
            show-action
            shape="round"
            placeholder="请输入公司名"
            @search="handleInpChange"
            enterkeyhint="搜索"
          >
            <template #action>
              <div @click="handleInpChange">搜索</div>
            </template>
          </van-search>
        </form>
      </div>
      <div class="list-quantity" v-if="listOfCompanies.length > 0">
        为你找到<span>{{ listOfCompanies.length }}</span
        >家公司
      </div>
      <div class="list-data">
        <div class="data-item" v-for="item in listOfCompanies" :key="item">
          <div class="item-name">
            <Hightlight
              class="hightlight-title"
              :highlight="{
                keyword: inputValue,
                caseSensitive: true,
                style: hightlightStyle,
              }"
            >
              {{ item.name }}
            </Hightlight>
          </div>
          <van-button plain type="info" @click="select(item.name)">选择</van-button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { getQueryCredit } from "@/api";
import Hightlight from "vue-words-highlight";
export default {
  name: "Search",
  components: { Hightlight },
  props: {
    type: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      inputValue: "", //输入框数据
      pageNum: 1, //页码
      historyData: [], //历史数据
      list: [], //搜索的数据
      loading: false, //下拉加载状态
      finished: false, //数据是否加载完
      hightlightStyle: {
        //关键字高亮样式
        color: "#ff0402",
        "font-weight": 400,
      },
      listOfCompanies: [], //搜索的公司列表数据
    };
  },
  methods: {
    //重置
    reset() {
      this.listOfCompanies = [];
      this.inputValue = "";
    },
    //选择按钮
    select(item) {
      this.$emit("select", { type: this.type, name: item });
    },
    //返回按钮
    cancel() {
      this.$emit("close");
    },
    /**
     * 输入框查询 查询时防抖
     */
    async handleInpChange() {
      const { items } = await getQueryCredit({
        companyName: this.inputValue,
      });
      this.listOfCompanies = items.filter((item) => item.matchType === "公司名称匹配");
    },
  },
  created() {
    this.listOfCompanies = [];
  },
};
</script>
<style lang="less" scoped>
.searchPage {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  .searchPage-topNavigationBar {
    width: 100%;
    height: 100px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: #ffffff;
    padding: 0 22px;
    box-sizing: border-box;
    background-color: #429df8;
    .topNavigationBar-return {
      height: 100%;
      display: flex;
      align-items: center;
      .iconfont {
        font-size: 32px;
        margin-top: 3px;
      }
    }
    .topNavigationBar-title {
      width: 400px;
      font-weight: 400;
      font-size: 36px;
      line-height: 54px;
      text-align: center;
    }
  }
  .searchPage-list {
    flex: 1;
    background: #f8f8fa;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    .list-search {
      background: #ffffff;
      box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.12);
    }
    .list-quantity {
      height: 96px;
      display: flex;
      align-items: center;
      font-size: 24px;
      color: #909399;
      font-weight: 400;
      margin-left: 20px;
      span {
        color: #f84242;
        margin: 0 10px;
      }
    }
    .list-data {
      flex: 1;
      padding: 0 20px;
      overflow-y: scroll;
      -webkit-overflow-scrolling: touch;
      &::-webkit-scrollbar {
        display: none;
      }
      .data-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 100px;
        border-bottom: 1px solid #eeeeee;
        .item-name {
          font-size: 28px;
          color: #303133;
          font-weight: 400;
        }
        .van-button {
          height: 52px;
          span {
            font-size: 24px;
            color: #429df8;
            font-weight: 500;
          }
        }
      }
    }
  }
}
</style>
